﻿class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            no: '',
            id: 0,
            counts: [],
            start: 1,
            pagesize: 15,
            keyword: "",
            giftlist: [],
            type: 0,
            state: 'ON',
            divtype: 1,
            typehtml: [],
            statehtml: [],
            merchantName: '',
            select_type: 1,
            scorelist: [],
            chosefile: !1,
            select_gift: 0,
            list_select_type:0,

        };
    }









    export() {

        let { keyword, size, price, cname, sshword } = this.state

        导出用户列表(res => {
            if (res.issuccess == 1) {

                let str = '用户id,注册时间,打卡积分,分享积分,小票获得积分,获取奖品次数,抽奖次数,当前积分\n'
                //`${topname}\n${housename}\n产品类型,高,宽,深度,单价,数量,总价,单位,立面说明,门板材质/柜体材质\n`
                const jsonData = []
                let total = 0;
                let totalcount = 0;

                // <td className="tconter">{((Number(x.height)* Number(x.width))/1000000).toFixed(2)}</td>
                //                                    <td className="tconter">{(Number(x.singleprice)* (((Number(x.height)* Number(x.width))/1000000).toFixed(2))).toFixed(2)}</td>
                res.list.forEach(x => {
                    str += `${x.id},${formatDate(new Date(x.updateDate))},${x.dakatime},${x.sharetime},${x.xiaopiaotime},${x.gifttime},${x.cjcount},${x.jf}\n`
                })
                let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
                let link = document.createElement("a");
                link.href = uri;
                link.download = formatDate(new Date()) + "用户列表.csv";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
        })

    }







    getdata1() {
        获取全部积分抽奖(res => {
            this.setState({ scorelist: res.list })
        })

    }

    getdata() {
        let { start, pagesize, keyword, size, price, cname, sshword,list_select_type } = this.state, that = this

        // 获取全部奖品(res => {
        //     if (res.issuccess == 1) {
        //         that.setState({ giftlist: res.list })
        //     }
        // })

        获取我的奖品列表({type:list_select_type, start: start == 0 ? 0 : start - 1, pagesize }, res => {
            let { list, total } = res;
            // let cnamehtml = []
            // cnamehtml.push(<option value={''}>请选择</option>)
            if (start == 1) {
                let pagecount = Math.ceil(total / pagesize);
                this.setpager(pagecount, start);
                this.setState({ start })
            }
            this.setState({ list, total })
        })

    }



    componentDidMount() {

        this.getdata()


        $('.modal1').hide()
        // testdingcan(res=>{
        // })
        this.setState({ showloading: '' })
        // this.freshlist()

    }




    setpager(pagecount, start) {
        let counts = [];
        for (let i = 0; i < pagecount; i++) {
            counts.push(start + i)
        }
        this.setState({ counts, pagestart: 0 })
    }
    clickpage(e) {
        let { pagesize } = this.state
        this.setState({ pagestart: e, start: e * pagesize + 1 }, () => {
            this.getdata()
        })
        console.log(e, 'eee');

        // if(start==e){
        //     start+=pagesize;
        // }else{
        //     start =start+ e*pagesize
        // }

        // this.setState({ pagestart: e, start }, () => {
        //    this.getdata()
        // })
    }
    setpage(e) {
        let { pagesize, start } = this.state
        pagesize = parseInt(e)
        start = 1
        this.setState({ pagesize, start }, () => {
            // 
            this.getdata()
        })
    }
    inputchange(prop, e) {
        this.setState({ [prop]: e.target.value })
    }

    datechange(e) {
        console.log(e.target.value, 'datechwange');
        // this.setState({ [prop]: e.target.value })
    }

    search() {
        this.setState({ start: 1 }, () => {
            this.getdata()
        })
    }
    closemodal() {
        $('.modal1').hide()
    }

    reset() {
        this.setState({ state: 'ON', no: '' }, () => {
            this.freshlist()
        })
    }
    selectchange(prop, e) {
        this.setState({ [prop]: e.target.value }, () => {
            this.search()
        })
    }

    selectfhchange(t, e) {
        console.log(t, e.target.value, 'sekle');
        this.setState({ [`select_${t == 'g' ? 'gift' : 'type'}`]: e.target.value })
    }
    listselectfhchange(e){
        this.setState({list_select_type:e.target.value},()=>{
            this.getdata()
        })
    }



    add() {
        let { select_type, select_gift, select_day, select_month, select_kc, giftlist } = this.state, that = this, err = '';


        console.log({ nowday, nowmonth }, '111');
        if (isNaN(select_day)) err = '日期请输入数字'
        if (isNaN(select_month)) err = '月份请输入数字'
        if (isNaN(select_kc)) err = '库存请输入数字'
        if (select_gift == 0) err = '请选择奖品'
        if (select_type != '2') select_type = 1;
        if (Number(select_kc) < 1) err = '库存应大于0'
        let nowday = new Date().getDate(), nowmonth = new Date().getMonth();
        if (Number(select_month) < nowmonth + 1) {
            err = '月份输入有误'
        } else if (Number(select_month) == nowmonth + 1 && Number(select_day) < nowday) {
            err = '日期输入有误'
        }
        console.log({ select_month, select_day }, '111');
        if (err != '') {
            alert(err)
        } else {
            let item = giftlist.find(x => { return x.id == select_gift })

            let data = { giftname: item.name, giftid: item.id, kc: select_kc, month: select_month, day: select_day, type: select_type }
            新建抽奖(data, res => {
                if (res.issuccess == 1) {
                    alert('新建成功')
                    $('.modal1').hide()
                    that.getdata()
                }
            })

            // 添加账号({ shopname: item.name, shopid: item.id, shopnum: select_usernum }, res => {
            //     $('.modal1').hide()
            //     if (res.issuccess == 1) {
            //         that.getdata()

            //     }
            // })
        }

    }


    del(id) {

        var a = confirm('确定要删除吗?'), that = this
        if (a) {
            删除我的奖品(id, res => {
                if (res.issuccess == 1) {
                    that.getdata() 
                }
            })
        }
    }

    render() {
        let { list, scorelist, select_month, divtype, select_gift, giftlist, select_day, select_type, select_kc,list_select_type } = this.state;

        return (
            <div className="breadLinebox">
                <div class="alertarea" id='alsucc' >
                </div>


                <Loading hidden={this.state.showloading != 'ON'} />





                <div className='panel-default content_wrap'>
                    <div className="breadLine ">
                        <div className="modal1" >
                            <div onClick={this.closemodal.bind(this)} className="mask"></div>
                            <div className="wrap">
                                <div className="content " >
                                    <div className="p1">选择奖品</div>
                                    <select value={select_gift} onChange={this.selectfhchange.bind(this, "g")}>
                                        <option value={0} >请选择</option>
                                        {

                                            giftlist.map((x, i) => {
                                                return (
                                                    <option key={i} value={x.id} >{x.name}</option>
                                                )
                                            })
                                        }
                                    </select>
                                </div>

                                <div className="content " >
                                    <div className="p1">选择类型</div>
                                    <select value={select_type} onChange={this.selectfhchange.bind(this, "t")}>
                                        <option value={1} >100积分</option>
                                        <option value={2} >1000积分</option>
                                    </select>
                                </div>


                                <div className="content">
                                    <p className="p1">月份</p>
                                    <antd.Input placeholder="请输入月份" allowClear value={select_month} onChange={this.inputchange.bind(this, 'select_month')} className="input" />
                                    {/* <p className="p2">{zje}</p> */}
                                </div>
                                <div className="content">
                                    <p className="p1">日期：天</p>
                                    <antd.Input placeholder="请输入几号" allowClear value={select_day} onChange={this.inputchange.bind(this, 'select_day')} className="input" />
                                    {/* <p className="p2">{zje}</p> */}
                                </div>
                                <div className="content">
                                    <p className="p1">库存</p>
                                    <antd.Input placeholder="请输入库存" allowClear value={select_kc} onChange={this.inputchange.bind(this, 'select_kc')} className="input" />
                                    {/* <p className="p2">{zje}</p> */}
                                </div>
                                <div className="btns">
                                    <p className="add" onClick={this.add.bind(this)}>确定</p>
                                    <p className="add" onClick={this.closemodal.bind(this)}>取消</p>
                                </div>
                            </div>

                        </div>
                        <div className="bbtn">
                            <p className={`button ${divtype == 1 ? 'l' : ''}`} onClick={() => {
                                // $('.modal1').show()
                                this.setState({ divtype: 1 })
                            }}  >奖品中奖</p>
                            <p className={`button ${divtype == 2 ? 'l' : ''}`} onClick={() => {
                                // $('.modal1').show()
                                this.getdata1()
                                this.setState({ divtype: 2 })
                            }}  >积分中奖</p>


                            <div style={{'display':divtype == 1 ? '' : 'none'}} className="content select" >
                                <div className="p1">选择中奖类型</div>
                                <select value={list_select_type} onChange={this.listselectfhchange.bind(this)}>
                                    <option value={0} >请选择</option>
                                    <option value={1} >积分抽奖</option>
                                    <option value={2} >惊喜礼盒</option>
                                   
                                </select>
                            </div>


                        </div>

                        {/* <input type="file" id='file1' onChange={this.filechange.bind(this)} style={{ display: "none" }} /> */}

                    </div>


                    <div style={{ 'display': divtype == 1 ? '' : 'none' }} className="contentBox">

                        <table className="tab">
                            <thead>
                                <tr>
                                    <th className="tconter">中奖时间</th>
                                    <th className="tconter">奖品名称</th>
                                    <th className="tconter">中奖人id</th>
                                    <th className="tconter">中奖类型</th>
                                    <th className="tconter">状态</th>
                                    <th className="tconter">操作</th>
                                    {/* <th className="tconter">操作</th> */}
                                    {/* <th>是否新人专享</th>
                                    <th>是否会员专享</th> */}
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    list.map((x, i) => {
                                        return (
                                            <tr key={i}>
                                                <td className="tconter">{formatDate(new Date(x.updateDate))}</td>
                                                <td className="tconter">{x.giftname}</td>
                                                <td className="tconter">{x.userid}</td>
                                                <td className="tconter">{x.lotteryid > 0 ? 
                                                ((x.lotterytype==2?'1000积分':'100积分' )+'抽奖'): x.surpriseid > 0 ? '惊喜礼盒' : '-'}</td>
                                                <td className="tconter">{x.state == 'UNCHECKED' ? '未兑换' : '已兑换'}</td>
                                                <td className="tconter">


                                                    <p style={{ 'color': 'red', 'text-decoration': 'underline', 'cursor': "pointer" }}
                                                        onClick={this.del.bind(this, x.id)}>删除</p>
                                                </td>


                                            </tr>
                                        )
                                    })
                                }
                            </tbody>
                        </table>
                        {this.state.wulist}
                        <div className="pagerbox">
                            <p>共{this.state.total}条</p>
                            <Pager counts={this.state.counts} click={this.clickpage.bind(this)} change={this.setpage.bind(this)} index={this.state.pagestart} />
                        </div>
                    </div>

                    <div style={{ 'display': divtype == 2 ? '' : 'none' }} className="contentBox">

                        <table className="tab">
                            <thead>
                                <tr>
                                    <th className="tconter">中奖时间</th>
                                    <th className="tconter">获得积分</th>
                                    <th className="tconter">中奖人id</th>
                                    <th className="tconter">操作</th>
                                    {/* <th className="tconter">操作</th> */}
                                    {/* <th>是否新人专享</th>
            <th>是否会员专享</th> */}
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    scorelist.map((x, i) => {
                                        return (
                                            <tr key={i}>
                                                <td className="tconter">{formatDate(new Date(x.updateDate))}</td>
                                                <td className="tconter">{x.score}</td>
                                                <td className="tconter">{x.userid}</td>
                                                <td className="tconter">

                                                    <p style={{ 'color': 'red', 'text-decoration': 'underline', 'cursor': "pointer" }}
                                                        onClick={this.del.bind(this, x.id)}>删除</p>
                                                </td>


                                            </tr>
                                        )
                                    })
                                }
                            </tbody>
                        </table>
                        {/* {this.state.wulist}
<div className="pagerbox">
    <p>共{this.state.total}条</p>
    <Pager counts={this.state.counts} click={this.clickpage.bind(this)} change={this.setpage.bind(this)} index={this.state.pagestart} />
</div> */}
                    </div>

                </div>
            </div>


        )


    }
}

ReactDOM.render(
    <div>
        <TopLine />
        <div id='body'>
            <LeftMenu code='mygift' height='mygift' />
            <Main />
        </div>
    </div>
    // <Barrage/>
    , $('.container')[0]
)